// document.querySelector('#btn-register').addEventListener('click', () => {
//     axios({
//         url: '/register',
//         method: 'post',
//         data: {
//             username: '10086',
//             password: '404'
//         }
//     }).then(result=>{
//         console.log(result);
//     })
// })

document.querySelector('#btn-register').addEventListener('click', async () => {
    const form = document.querySelector('.register-form')
    const data = serialize(form, { empty: true, hash: true })
    // console.log(data)
    const { username, password } = data
    // console.log(username, password);
    //非空校验
    if (username === '' || password === '') {
        showToast("用户名和密码不能为空")
        return
    }
    //长度校验
    if (username.length < 8 || username.length > 20 || password.length < 6 || password.length > 20) {
        showToast("用户名长度8-20，密码长度6-20")
        return
    }

    try{
        const res = await axios.post('/register',{username,password})
        showToast(res.message)
    }catch(error){
        showToast(error.response.data.message)
    }
})